package com.njdj.gps.common.util;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class PubXMLConfig {

	private static String XML_FILENAME = "/PubConfig.xml";
	private static PubXMLConfig instance;
	private static Document doc;

	private PubXMLConfig() {
	}

	public static PubXMLConfig getInstance() {
		if (instance == null) {
			instance = new PubXMLConfig();
		}
		try {
			URL url = PubXMLConfig.class.getResource(XML_FILENAME);
			SAXReader saxReader = new SAXReader();
			doc = saxReader.read(url);
			doc.normalize();
		} catch (DocumentException ex) {
			ex.printStackTrace();
		}
		return instance;
	}

	public List<String[]> listPubXmlConfigAll() {
		List<String[]> list=new ArrayList<String[]>();
		List elements = doc.getRootElement().element("Items").elements("Item");
		for (Iterator iterator = elements.iterator(); iterator.hasNext();) {
			Element element = (Element) iterator.next();
			String[] str=new String[2];
			str[0]=element.elementText("PortName");
			str[1]=element.elementText("PortFolioid");
			list.add(str);
		}
		return list;
	}

	public static void main(String[] args) {
		PubXMLConfig config = PubXMLConfig.getInstance();
		config.listPubXmlConfigAll();
	}

}
